package algorithm.lru;

import java.util.*;
import java.util.stream.Stream;

public class ListLRU {

    public void lru(LinkedList list, int volume, int value) {
        while (list.size() > volume) {
            list.removeLast();
        }

        if(list.contains(value)) {
            int index = list.indexOf(value);
            list.remove(index);
            list.add(0, value);
        }

        if(!list.contains(value)) {
            list.add(0, value);
        }
    }
    public static void main(String []args) {
        ListLRU lru = new ListLRU();
        LinkedList list = new LinkedList();
        int []array = new int[]{1,2,3,1,2,1,5,3,3,2,6,7};
        Arrays.stream(array).forEach(item->{
            lru.lru(list, 6, item);
        });

        Stream.of(list).forEach(item->{
            System.out.println(item+",");
        });

    }
}
